<?php

namespace App\Service;

use App\Models\EwMemberPraise;

class MemberPraiseService extends BaseService
{
	const UP = 1;//1、点赞
	const DOWN = -1;//-1、点踩

	public static function getList($params, $count = FALSE)
	{
		$builder = new \Phalcon\Mvc\Model\Query\Builder();
		$builder->columns('p.id,p.memebr_id,m.nickname,p.table_id.p.list_id,p.praise');
		$builder->addForm('App\Models\EwMemberPraise', 'p');
		$builder->leftJoin('App\Models\EwMember', 'p.memebr_id, = m.id', 'm');

		if ($count){
			return $builder->getQuery()->execute()->count();
		}else{
			$builder->limit($params->page_num,($params->p-1)*$params->page_num);
			$result = $builder->getQuery();
			return $result->execute();
		}		
	}

	public static function getInfo($id = 0)
	{
		$builder = new \Phalcon\Mvc\Model\Query\Builder();
		$builder->columns('p.id,p.memebr_id,m.nickname,p.table_id.p.list_id,p.praise');
		$builder->addForm('App\Models\EwMemberPraise', 'p');
		$builder->leftJoin('App\Models\EwMember', 'p.memebr_id, = m.id', 'm');

		if (isset($params->id) && $params->id > 0)
			$builder->andwhere("c.id = :id:")->bind(['id' => $params->id]);

		$result = $builder->getQuery()->execute()->toArray();

		$data = new \stdClass();
		if (is_array($result) && isset($result[0]) && is_array($result[0])){
			foreach ($result[0] as $key => $value) {
				$data->$key = $value;
			}
		}
		return $data;
	}

	public static function count()
	{
		return EwMemberPraise::count();
	}

	/**
	 * [getPraiseInfo 判断是否已经点赞/点踩]
	 * @param  [type] $member_id     [会员id]
	 * @param  [type] $table_id      [数据表id]
	 * @param  [type] $list_id       [数据列id]
	 * @param  [type] $praise    [1、点赞 -1、点踩]
	 * @return [type]            [description]
	 */
	public static function getPraiseInfo($member_id, $table_id, $list_id, $praise = 1)
	{	
		return EwMemberPraise::findFirst(['member_id = ' . $member_id . ' AND table_id = ' . $table_id . ' AND list_id = ' . $list_id . ' AND praise = ' . $praise]);
	}

	/**
	 * [jugePraiseArr 判断是否点赞]
	 * @param  [type]  $member_id [description]
	 * @param  [type]  $table_id  [description]
	 * @param  [type]  $id_arr    [description]
	 * @param  integer $praise    [description]
	 * @return [type]             [description]
	 */
	public static function jugePraiseArr($member_id, $table_id, $list_id_arr, $praise = 1)
	{
		$list_id_str = implode($list_id_arr, ',');
		$data = EwMemberPraise::find(['columns' => 'list_id', 'conditions' => 'member_id = ' . $member_id . ' AND table_id = ' . $table_id . ' AND list_id in (' . $list_id_str . ') AND praise = ' . $praise])->toArray();
		return array_map('array_shift',$data);
	}

	public static function save($data)
	{
		//判断是否已经点赞/点踩
		$info = self::getPraiseInfo($data->member_id, $data->table_id, $data->list_id, $data->praise);

		if ($info)
			return FALSE;

		$row = new EwMemberPraise();
		return $row->save((array)$data);
	}

	/**
	 * [do 会员 点赞/点踩/取消点赞/取消点踩 ]
	 * @param  [type] $data->member_id 		[会员id]
	 * @param  [type] $data->table_id       [数据表id]
	 * @param  [type] $data->list_id        [数据列id]
	 * @param  [type] $data->Praise    		[1、点赞 -1、点踩]
	 * @return [type]                  		[description]
	 */
	public static function do($data)
	{

		$info = self::getPraiseInfo($data->member_id, $data->table_id, $data->list_id, $data->praise);

		if ($info && $info->delete()){//取消点赞
			return "delete";
		}else{//点赞
			$model = new EwMemberPraise();
			$model->save((array)$data);
			return "save";
		}
	}

	public static function juge($data)
	{
		if (!$data->member_id)
			return -1;
		
		if ($data->praise != self::DOWN)
			$data->praise = self::UP;

		$info = self::getPraiseInfo($data->member_id, $data->table_id, $data->list_id, $data->praise);
		if ($info)
			return 1;
		return -1;
	}

	/**
	 * [sum 统计当前列表点踩点赞数量]
	 * @param  [type]  $table_id   [数据表id]
	 * @param  [type]  $list_id    [数据列id]
	 * @param  boolean $praise 	   [TRUE 点赞 FALSE 点踩]
	 * @return [type]          [description]
	 */
	public static function sum($data)
	{
		if ($data->praise != self::DOWN)
			$data->praise = self::UP;
		
		return EwMemberPraise::count([
				"table_id = :table_id: AND list_id = :list_id: AND praise = :praise:",
				"bind" => [
					"table_id" => $data->table_id,
					"list_id"  => $data->list_id,
					"praise"   => $data->praise
				],
			]);
	}

	/**
	 * [delete 彻底删除]
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	public static function forever($id)
	{
		if (isset($id) && $id)
			$row = EwMemberPraise::findFirst($id);

		if ($row)
			return $row->delete();

		return NULL;
	}
}